package com.huawang.mdesign.model.service.controller;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.huawang.business.vo.ModelResultVo;
import com.huawang.business.vo.SelectElementNode;
import com.huawang.mdesign.model.service.config.WebLogIgnore;
import com.huawang.mdesign.model.service.dto.CreateInstanceSpecificationDTO;
import com.huawang.mdesign.model.service.dto.ResolveFmuDTO;
import com.huawang.mdesign.model.service.inter.FmuService;
import com.huawang.mdesign.model.service.inter.SimulationService;
import com.huawang.mdesign.model.service.vo.BaseResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Tag(name = "仿真业务")
@RestController
@RequestMapping("/simulation")
@Validated
@Slf4j
public class SimulationController {

    @Autowired
    private SimulationService simulationService;

    @Autowired
    private FmuService fmuService;

    @Operation(summary = "查询项目内创建的package，树状返回")
    @GetMapping("queryPackages")
    public BaseResponse<SelectElementNode> queryPackages(String projectId) {
        return BaseResponse.success(simulationService.queryPackages(projectId));
    }

    @Operation(summary = "查询项目内类型对应的实例，树状返回")
    @GetMapping("queryInstancesByClassifier")
    public BaseResponse<SelectElementNode> queryInstancesByClassifier(String projectId, String classifierId) {
        return BaseResponse.success(simulationService.queryInstancesByClassifier(projectId, classifierId));
    }

    @Operation(summary = "查询元素是否支持仿真")
    @GetMapping("checkElementSupportEmulation")
    public BaseResponse<Boolean> checkElementSupportEmulation(String projectId, String instanceId) {
        return BaseResponse.success(simulationService.checkElementIsSupportEmulation(projectId, instanceId));
    }

    @Operation(summary = "查询ConnectUrl")
    @GetMapping("queryConnectUrl")
    public BaseResponse<String> queryConnectUrl(String projectId) {
        return BaseResponse.success(simulationService.queryConnectUrl(projectId));
    }

    @Operation(summary = "查询是否有模型变化，并初始化")
    @GetMapping("checkModelChange")
    public BaseResponse<Boolean> checkModelChange(String projectId) {
        return BaseResponse.success(simulationService.checkModelChange());
    }

    @Operation(summary = "解析FMU")
    @PostMapping("fmu/resolution")
    public BaseResponse<ObjectNode> resoluteFMU(@RequestBody ResolveFmuDTO resolveFmuDTO) {
        return BaseResponse.success(fmuService.resolveFMU(resolveFmuDTO.getProjectId(), resolveFmuDTO.getFilePath()));
    }

    @Operation(summary = "查询仿真数据，JSON结构返回")
    @WebLogIgnore
    @GetMapping("querySimulationJsonData")
    public BaseResponse<String> querySimulationJsonData(@RequestParam(value = "projectId") String projectId,
                                                        @RequestParam(value = "instanceId") String[] instanceId) {
        return BaseResponse.success(simulationService.querySimulationJsonData(projectId, Arrays.asList(instanceId)));
    }

    @Operation(summary = "查询仿真数据,分页返回，JSON结构返回")
    @WebLogIgnore
    @GetMapping("querySimulationJsonDataByPage")
    public BaseResponse<String> querySimulationJsonDataByPage(@RequestParam(value = "projectId") String projectId,
                                                              @RequestParam(value = "instanceId") String[] instanceId,
                                                              @RequestParam(value = "mapType") String mapType,
                                                              @RequestParam(value = "pageNum") Integer pageNum,
                                                              @RequestParam(value = "pageSize") Integer pageSize,
                                                              @RequestParam(value = "uuid") String uuid) {
        return BaseResponse.success(simulationService.querySimulationJsonDataByPage(Arrays.asList(instanceId),
                mapType, pageNum, pageSize, uuid));
    }

    @Operation(summary = "查询仿真数据数量")
    @WebLogIgnore
    @GetMapping("querySimulationJsonDataCount")
    public BaseResponse<String> querySimulationJsonDataCount(@RequestParam(value = "projectId") String projectId,
                                                             @RequestParam(value = "instanceId") String[] instanceId,
                                                             @RequestParam(value = "start") String start,
                                                             @RequestParam(value = "uuid") String uuid) {
        return BaseResponse.success(simulationService.querySimulationJsonDataCount(projectId, Arrays.asList(instanceId), start, uuid));
    }


    @Operation(summary = "实例表仿真——更新实例值")
    @PostMapping("instanceTableSimulation/updateInstanceSpecificationValue")
    public BaseResponse<ModelResultVo> updateInstanceSpecificationValue(@RequestBody CreateInstanceSpecificationDTO createInstanceSpecificationDto) {
        return BaseResponse.success(simulationService.updateInstanceSpecificationValue(createInstanceSpecificationDto));
    }
}
